#include <math.h>

#define THRESH1 0.075   // almost no activity
#define THRESH2 0.1     // low activity
#define THRESH3 0.15    // medium activity
#define THRESH4 0.3     // high activity

int lookupActivityLevel(float activity)
{
    if(activity > THRESH4)
        return 4;
    if(activity > THRESH3)
        return 3;
    if(activity > THRESH2)
        return 2;
    if(activity > THRESH1)
        return 1;
    else
        return 0;
}

// takes an array of raw accelerometer samples and the length of the array and returns
// the corresponding discrete activity level defined by the average activity level of
// the samples.  
int calcActivityLevel(float samples[][3], int length)
{
    int i;
    float magnitude = 0, temp;
        
    // find average normalized magnitude
    for(i = 0; i < length; i++)
    {
        temp = samples[i][0]*samples[i][0] + samples[i][1]*samples[i][1] + samples[i][2]*samples[i][2];
        temp = fabs(sqrt(temp) - 1);
        magnitude += temp;
    }
    magnitude /= length;
    
    // find and return threshold level
    return lookupActivityLevel(magnitude);
}
